package newcrunch.typography;

import java.awt.Color;
import java.util.Arrays;
import javax.swing.JDialog;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;
import newcrunch.util.PropertyLoader;

/**
 * @author WL
 */
public class TypoDetailsDialog extends JDialog
{
    private JTable displayTable;
    private JScrollPane scrollPane;
    private TypoDetailsModel<String> model;
    private KeyHighlighter highlighter;
    private String[] headers;
    
    private static Color color1 = new Color(237, 243, 254);
    private static Color color2 = Color.WHITE;
    
    static {
        color1 = PropertyLoader.getColorProperty("color3");
        color2 =PropertyLoader.getColorProperty("color4");
    }
    
    public TypoDetailsDialog(String title, String[] headers)
    {
        model = new TypoDetailsModel<String>(headers);
        this.headers = Arrays.copyOf(headers, headers.length);
        
        setTitle(title);
        
        initDisplay();
        
        setListeners();
        
        initTable();
    }
    
    public void reset()
    {
        model = new TypoDetailsModel<String>(headers);
        displayTable.setModel(model);
        displayTable.getSelectionModel().clearSelection();
    }
    
    public void refresh()
    {
        displayTable.revalidate();
        pack();
    }
    
    @Override
    public void setVisible(boolean val)
    {
        if (val)
        {
            refresh();

            if (highlighter != null)
                highlighter.updateHighlights();
            
            processTableListener();
        }
        
        else if (highlighter != null)
            highlighter.removeHighlights();
            
        
        super.setVisible(val);
    }
    
    /**
     * Sets the highlighter from which the entries of this dialog
     * will highlight on the editor.
     * @param highlighter 
     */
    public void setHighlighter(KeyHighlighter highlighter)
    {
        if (this.highlighter != null)
            this.highlighter.removeHighlights();
        
        this.highlighter = highlighter;
    }
    
    public KeyHighlighter getHighlighter()
    {
        return highlighter;
    }
    
    public void addRow(String... args)
    {
        model.addRow(args);
    }
    
    public int getRowCount()
    {
        return model.getRowCount();
    }
    
    public String getValueAt(int row, int column)
    {
        return (String)model.getValueAt(row, column);
    }
    
    public void processTableListener()
    {
        int selectedRow;
        if ((selectedRow = displayTable.getSelectedRow()) < 0)
            return;

        int index = displayTable.convertRowIndexToModel(selectedRow);
        if (index >= 0)
            highlighter.jumpToHighlight(index);
    }
    
    private void setListeners()
    {        
        displayTable.getSelectionModel().addListSelectionListener(new ListSelectionListener(){
            @Override
            public void valueChanged(ListSelectionEvent e) {
                TypoDetailsDialog.this.processTableListener();
            }
        });
    }
        
    public void initTable()
    {
        displayTable.setModel(model);
        
        TableRowSorter sorter = (TableRowSorter)displayTable.getRowSorter();
        TypoDetailsComparator comparator = new TypoDetailsComparator();
        for (int i = 0; i < model.getColumnCount(); i++)
            sorter.setComparator(i, comparator);
    }
    
    private void initDisplay()
    {
        displayTable = new JTable() {
            @Override
            public TableCellRenderer getCellRenderer(int row, int column) 
            {
                DefaultTableCellRenderer renderer = new DefaultTableCellRenderer();
                
                if (row % 2 == 0)
                    renderer.setBackground(color1);
                else
                    renderer.setBackground(color2);
                
                return renderer;
            }
        };
        displayTable.setShowHorizontalLines(false);
        displayTable.setShowVerticalLines(true);
        displayTable.setAutoCreateRowSorter(true);
        
        //set the sorting
        TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>();
        displayTable.setRowSorter(sorter);
                        
        scrollPane = new JScrollPane();

        javax.swing.GroupLayout typoDetailsDialogLayout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(typoDetailsDialogLayout);
        typoDetailsDialogLayout.setHorizontalGroup(
            typoDetailsDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(scrollPane, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 430, Short.MAX_VALUE)
        );
        typoDetailsDialogLayout.setVerticalGroup(
            typoDetailsDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(scrollPane, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 388, Short.MAX_VALUE)
        );
        
        scrollPane.setViewportView(displayTable);        
        
        
    }
}
